import api from '../../../api/index'
import store from '../../../store/index'
// pages/contest/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '123',//当前比赛的id
    statusList: ['队员招募', '寻找团队'],
    activeIndex: 0,
    showPopout: false,
    toView: 'toComment1',
    contest: {
      name: '“创客杯”大学生创业大赛',
      title: '“创客杯”大学生创业大赛正在火热报名中！',
      image: '/images/华农1.jpg',
      introduction: '中国国际互联网+大学生创新创业大赛的华农校赛',
      detail: '', //活动详情
      rule: '',//报名及比赛资讯
      timeEnd: '报名时间：2020-04-21',//比赛时间,
      url: '',//推文跳转链接
    },
    teams: [],
    player: [],
    findTeamMate: [
      {
        avatarUrl: '/images/华农1.jpg',
        nickName: '陈大杰',
        content: '起参加比比赛找人一起参加比赛找人一加比赛找人一起参加比赛找人一起参加比赛找人一起参加比赛，已经有4个人了，冲赛，已经有4个人了，冲赛，已经有4个人了，冲赛，已经有4个人了',
        createTime: '2020-04-20',
        currNum: 3,
        needNum: 8,
        name: '“创客杯”大学生创业大赛',
        teamName: 'kyc。com',
        needIntroduction: '牛逼就行'
      }
    ],
    findCaptain: [
      {
        avatarUrl: '/images/华农1.jpg',
        nickName: '陈大杰',
        introduction: '起参加比比赛找人一起参加比赛找人一加比赛找人一起参加比赛找人一起参加比赛找人一起参加比赛，已经有4个人了，冲赛，已经有4个人了，冲赛，已经有4个人了，冲赛，已经有4个人了',
        createTime: '2020-04-20',
        currNum: 3,
        needNum: 8,
        skill: [],//技能
        name: '“创客杯”大学生创业大赛'
      }
    ],
    interest: false // 是否收藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let contest = decodeURIComponent(options.contest)

    if (options != undefined) {
      this.getContest(contest).then(res => {
        this.teamList()
        this.checkCollect()
      })
    } else {
      this.teamList()
      this.checkCollect()
    }

  },
  getContest: async function (contest) {

    return new Promise((reslove, reject) => {
      // console.log(options.id)
      var id = contest._id

      if (id !== undefined) {
        var params = { id }
        api.contest.getContest(params).then(res => {
          console.log(res);
          this.setData({
            contest: res.data
          }, () => {
            reslove("sucecess")
          })
        })
      } else {
        console.log(JSON.parse(contest))
        this.setData({
          contest: JSON.parse(contest)
        }, () => {
          reslove("sucecess")
        })
      }
    })
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    console.log('下拉刷新');
    this.teamList()
    this.checkCollect()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新


  },
  onShow: function () {

  },
  flashComp: function () {
    this.setData({
      activeIndex: !this.data.activeIndex
    })
    this.setData({
      activeIndex: !this.data.activeIndex
    })
  },
  navbarChange: function (e) {
    var activeIndex = e.detail.index
    this.setData({
      activeIndex
    })
    console.log(activeIndex)
    if (activeIndex == 2) {
      this.setData({
        showPopout: true
      })
    }
  },
  teamList: function () {

    var id = this.data.contest._id
    var status = 'all'
    var params = { id, status }

    api.team.list(params).then(res => {
      this.setData({
        teams: res.result.data.list
      }, () => {
        this.flashComp()
      })
    })

    api.player.list(params).then(res => {
      this.setData({
        player: res.result.data.list
      }, () => {
        this.flashComp()
      })
    })

  },
  toComment: function () {
    var id = this.data.contest._id
    var name = this.data.contest.name
    wx.showActionSheet({
      itemList: ['队员招募', '寻找团队'],//显示的列表项
      success: function (res) {//res.tapIndex点击的列表项
        var nickName = store.data.user.nickName
        if (nickName == '') {
          wx.showToast({
            title: '请先登录后使用该功能',
          })
          return 0
        }
        var index = res.tapIndex
        console.log(index)
        console.log(id)
        wx.navigateTo({
          url: '/pages/team/publish/publish?index=' + index + '&id=' + id + '&name=' + name,
        })
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  tapLink: function () {
    // wx.showToast({
    //   title: '功能正在维护中',
    //   icon:'error'
    // })
    // wx.miniProgram.navigateTo({url: 'https://mp.weixin.qq.com/s/sApJsl7rl-tKgGtU98QbZg'})
    if (this.data.contest.url) {
      wx.navigateTo({
        url: "/pages/contest/tuiWen/tuiWen?url=" + this.data.contest.url,
      })
    } else {
      wx.showToast({
        title: '暂时无相关推文',
        icon: 'error'
      })
    }

  },
  tapTeam: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var team = this.data.teams[index]
    var teamID = team._id
    console.log(team)
    wx.navigateTo({
      url: '../../team/detail/detail?team=' + JSON.stringify(team) + '&teamID=' + teamID,
    })
  },
  tapPlayer: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var player = this.data.player[index]
    console.log(player)
    wx.navigateTo({
      url: '../../team/player/player?player=' + JSON.stringify(player),
    })
  },
  // 处理收藏函数
  handleInterest() {
    var id = this.data.contest._id
    var params = { id }
    this.setData({
      interest: !this.data.interest
    }, () => {
      if (this.data.interest) {
        api.userInfo.collect(params).then(res => {
          store.refreshUser()
        })
      }
      if (!this.data.interest) {
        api.userInfo.uncollect(params).then(res => {
          console.log(res)
          store.refreshUser()
        })
      }
    })

    // 提示用户
    wx.showToast({
      title: this.data.interest ? '收藏成功' : '取消收藏',
      icon: 'success'
    })
  },
  checkCollect: function () {
    var collect = store.data.user.collect
    var id = this.data.contest._id
    console.log(collect)
    var index = collect.indexOf(id)
    if (index != -1) {
      this.setData({
        interest: true
      })
    }
  }
})